/**
 * 
 */
package net.viralpatel.struts2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadCustommerCSVFileAction extends ActionSupport {

	private InputStream fileInputStream;
	private int year;
	private int month;

	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	public String execute() throws Exception {

		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "t10";
		String driverName = "com.mysql.jdbc.Driver";
		Connection conn = null;
		ResultSet resultSet = null;
		String nameRoot = "root";
		String passwordRoot = "";
		String fileName = "C:\\Users\\KAKA\\workspace\\WCAMS\\WebContent\\csv\\CustommerCSVFile.csv";

		try {
			FileWriter fw = new FileWriter(fileName);
			Class.forName(driverName);
			conn = DriverManager.getConnection(url + dbName, nameRoot,
					passwordRoot);
			String query = "SELECT userID, room.roomID,day, startTime, endTime,flagCancel,payStatus,cost FROM yoyaku, room WHERE yoyaku.roomID = room.roomID order by userID";
			PreparedStatement prepStmt = conn.prepareStatement(query);
			resultSet = prepStmt.executeQuery();
			if (resultSet.next()) {
				resultSet.beforeFirst();
				int i = 1;
				while (resultSet.next()) {
					String date = resultSet.getString(3);
					String[] temp = date.split("-");
					if (Integer.parseInt(temp[1]) == month
							&& Integer.parseInt(temp[2]) == year) {
						fw.append(Integer.toString(i));
						fw.append(',');
						fw.append(resultSet.getString(1));// userID
						fw.append(',');
						fw.append(resultSet.getString(2));// roomID
						fw.append(',');
						fw.append(resultSet.getString(3));// day
						fw.append(',');
						fw.append(resultSet.getString(4));// startTime
						fw.append(',');
						fw.append(resultSet.getString(5));// endTime
						fw.append(',');
						if (resultSet.getInt(6) == 0) {
							if (resultSet.getInt(7) == 0) {
								fw.append(resultSet.getString(8));// cost
							}
						} else {
							if (resultSet.getInt(7) == 0) {
								fw.append("5000");// cost
							}
						}
						fw.append('\n');
						i++;
					}
				}
				fw.flush();
				fw.close();
				conn.close();
			} else {
				conn.close();
				return ERROR;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		fileInputStream = new FileInputStream(new File(fileName));

		return SUCCESS;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

}

/*
 * String[] months = {"January", "February", "March", "April", "May", "June",
 * "July", "August", "September", "October", "November", "December"};
 * 
 * Calendar cal = Calendar.getInstance(); String month =
 * months[cal.get(Calendar.MONTH)]; System.out.println("Month name: " + month);
 */